package ink.onei.common.core.domain.model;

import com.alibaba.fastjson2.annotation.JSONField;
import ink.onei.common.core.domain.entity.Seller;
import ink.onei.common.core.domain.entity.SysUser;
import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.io.Serial;
import java.util.Collection;
import java.util.Set;

/**
 * 登录用户身份权限
 *
 * @author nekotako
 */

@Data
public class LoginUser implements UserDetails {
    @Serial
    private static final long serialVersionUID = 1L;

    // 用户ID
    private Long id;

    // 部门ID
    private Long deptId;

    // 用户唯一标识
    private String token;

    // 用户类型
    private String userType;

    // 登录时间
    private Long loginTime;

    // 过期时间
    private Long expireTime;

    // 登录IP地址
    private String ipaddr;

    // 登录地点
    private String loginLocation;

    // 浏览器类型
    private String browser;

    // 操作系统
    private String os;

    // 权限列表
    private Set<String> permissions;

    // 用户信息
    private SysUser user;

    // 商家信息
    private Seller seller;

    public LoginUser() {
    }

    public LoginUser(Seller seller, Set<String> permissions) {
        this.seller = seller;
        this.permissions = permissions;
        this.userType = "seller";
    }

    public LoginUser(Long id, Seller seller, Set<String> permissions) {
        this.id = id;
        this.seller = seller;
        this.permissions = permissions;
        this.userType = "seller";
    }

    public LoginUser(SysUser user, Set<String> permissions) {
        this.user = user;
        this.permissions = permissions;
        this.userType = "system";
    }

    public LoginUser(Long id, Long deptId, SysUser user, Set<String> permissions) {
        this.id = id;
        this.deptId = deptId;
        this.user = user;
        this.permissions = permissions;
        this.userType = "system";
    }

    @JSONField(serialize = false)
    @Override
    public String getPassword() {
        if (user != null) return user.getPassword();
        else if (seller != null) return seller.getPassword();
        return null;
    }

    @Override
    public String getUsername() {
        if (user != null) return user.getUserName();
        else if (seller != null) return seller.getUsername();
        return null;
    }

    // 账户是否未过期,过期无法验证
    @JSONField(serialize = false)
    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    // 指定用户是否解锁,锁定的用户无法进行身份验证
    @JSONField(serialize = false)
    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    // 指示是否已过期的用户的凭据(密码),过期的凭据防止认证
    // @return
    @JSONField(serialize = false)
    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    // 是否可用 ,禁用的用户不能身份验证
    // @return
    @JSONField(serialize = false)
    @Override
    public boolean isEnabled() {
        return true;
    }


    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return null;
    }
}
